#ifndef __MY_DATAMANAGER__
#define __MY_DATAMANAGER__

#include "Util.hpp"
#include <unordered_map>

namespace hhbcloud
{
    class DataManager
    {
    public:
        DataManager(const std::string& backup_file)
            :_backup_file(backup_file)
        {
            InitLoad();
        }

        int Split(const std::string& body, const std::string& sep, std::vector<std::string>* array)
        {
            int count = 0;
            int start = 0;
            while (1)
            {
                size_t pos = body.find(sep, start);
                if (pos == std::string::npos)
                {
                    break;
                }
                if (start == pos)
                {
                    start = start + sep.size();
                    continue;
                }
              
                array->push_back(body.substr(start, (pos - start) + 1));
                count++;
                start = pos + sep.size();
            }
            if (start < body.size())
            {
                array->push_back(body.substr(start));
                count++;
            }

            return count;
                
        }

        bool InitLoad()
        {
            FileUtil fu(_backup_file);

            if (fu.Exists() == false)
            {
                std::cout << "client Data manager file is not exists" << std::endl;
                return false;
            }
            
            std::string body;
            fu.GetContent(&body);

            std::vector<std::string> array;
            Split(body, "\n", &array);

            for (auto& e : array)
            {
                std::vector<std::string> tmp;
                Split(e, " ", &tmp);

                if (tmp.size() != 2)
                {
                    continue;
                }

                _table[tmp[0]] = tmp[1];
            }

            return true;
        }


        bool Storage()
        {
            std::stringstream ss;
            auto it = _table.begin();
            for (; it != _table.end(); ++it)
            {
                ss << it->first << " " << it->second << "\n";
            }

            FileUtil fu(_backup_file);
            fu.SetContent(ss.str());
            return true;
        }

        bool Insert(const std::string& key, const std::string& val)
        {
            _table[key] = val;
            Storage();
            return true;
        }


        bool Update(const std::string& key, const std::string& val)
        {
            _table[key] = val;
            Storage();
            return true;
        }

        bool GetOneByKey(const std::string& key, std::string* val)
        {
            auto it = _table.find(key);
            if (it == _table.end())
            {
                return false;
            }

            *val = it->second;
            return true;
        }

    private:
        std::string _backup_file;
        std::unordered_map<std::string, std::string> _table;
    };
}



#endif